#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class CourseProvider : CY.GFive.Core.Providers.DALProvider.ICourseProvider
	{
		#region Sql
		private static readonly string SqlSelectCourse = "SELECT [Id], [Code],[Name],[Des],[CourseNum],[CourseScore] FROM [Course] WHERE  [Id]=@Id";

		private static readonly string SqlInsertCourse = "INSERT INTO [Course]([Code],[Name],[Des],[CourseNum],[CourseScore]) values(@Code,@Name,@Des,@CourseNum,@CourseScore);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateCourse = "UPDATE [Course] SET [Code] = @Code,[Name] = @Name,[Des] = @Des,[CourseNum] = @CourseNum,[CourseScore] = @CourseScore WHERE  [Id]=@Id";

		private static readonly string SqlDeleteCourse = "DELETE [Course] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllCourse = "SELECT [Id], [Code],[Name],[Des],[CourseNum],[CourseScore] FROM [Course]";
		#endregion

		#region ICourseProvider Members
		public Core.Business.Course Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectCourse);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.Course course = new Core.Business.Course();

				if (!reader.IsDBNull(0)) course.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) course.Code = reader.GetString(1);
				if (!reader.IsDBNull(2)) course.Name = reader.GetString(2);
				if (!reader.IsDBNull(3)) course.Des = reader.GetString(3);
				if (!reader.IsDBNull(4)) course.CourseNum = reader.GetInt32(4);
				if (!reader.IsDBNull(5)) course.CourseScore = reader.GetInt32(5);

				return course;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.Course course)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Code", SqlDbType.NVarChar, course.Code);
			sql.AddParameter("@Name", SqlDbType.NVarChar, course.Name);
			sql.AddParameter("@Des", SqlDbType.NVarChar, course.Des);
			sql.AddParameter("@CourseNum", SqlDbType.Int, course.CourseNum);
			sql.AddParameter("@CourseScore", SqlDbType.Int, course.CourseScore);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertCourse);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) course.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.Course course)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Code", SqlDbType.NVarChar, course.Code);
			sql.AddParameter("@Name", SqlDbType.NVarChar, course.Name);
			sql.AddParameter("@Des", SqlDbType.NVarChar, course.Des);
			sql.AddParameter("@CourseNum", SqlDbType.Int, course.CourseNum);
			sql.AddParameter("@CourseScore", SqlDbType.Int, course.CourseScore);
			sql.AddParameter("@Id", SqlDbType.Int, course.Id);
			sql.ExecuteSql(SqlUpdateCourse);
		}

		public void Delete(Core.Business.Course course)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, course.Id);
			sql.ExecuteSql(SqlDeleteCourse);
		}

		public IList<Core.Business.Course> GetAllCourse()
		{
			IList<Core.Business.Course> courselist = new List<Core.Business.Course>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllCourse);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.Course course = new Core.Business.Course();

					if (!reader.IsDBNull(0)) course.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) course.Code = reader.GetString(1);
					if (!reader.IsDBNull(2)) course.Name = reader.GetString(2);
					if (!reader.IsDBNull(3)) course.Des = reader.GetString(3);
					if (!reader.IsDBNull(4)) course.CourseNum = reader.GetInt32(4);
					if (!reader.IsDBNull(5)) course.CourseScore = reader.GetInt32(5);

					course.MarkOld();
					courselist.Add(course);
				}
				reader.Close();
			}
			return courselist;
		}
		#endregion
	}
}
